package controllers

import (
	"encoding/json"
	beego "github.com/beego/beego/v2/server/web"
	"strconv"
	"time"
	"yyy/models"
	"yyy/pkg"
)

type UserControllers struct {
	beego.Controller
}

type Msg struct {
	Code int         `json:"code"`
	Msg  string      `json:"msg"`
	Data interface{} `json:"data"`
}

func (u *UserControllers) Log() {
	name := u.GetString("name")
	password := u.GetString("password")
	if name == "" || password == "" {
		u.Data["json"] = Msg{
			Code: 0,
			Msg:  "用户名密码不能为空",
			Data: nil,
		}
		u.ServeJSON()
		return
	}
	user_redis_key := "user_log" + name
	cacheJson, _ := models.Rdb.Get(models.Ctx, user_redis_key).Int()
	if cacheJson >= 3 {
		u.Data["json"] = Msg{
			Code: 0,
			Msg:  "密码错误三次或三次以上,禁止登录",
			Data: nil,
		}
		u.ServeJSON()
		return
	}
	var user models.User
	err := models.DB.Where("name = ?", name).First(&user).Error
	if err != nil {
		u.Data["json"] = Msg{
			Code: 0,
			Msg:  "用户名不存在",
			Data: nil,
		}
		u.ServeJSON()
		return
	}

	if user.Password != pkg.Md5(password) {
		models.Rdb.Incr(models.Ctx, user_redis_key)
		if cacheJson == 2 {
			models.Rdb.Set(models.Ctx, user_redis_key, 3, time.Hour)
			u.Data["json"] = Msg{
				Code: 0,
				Msg:  "密码错误三次",
				Data: nil,
			}
			u.ServeJSON()
			return
		}
		u.Data["json"] = Msg{
			Code: 0,
			Msg:  "密码错误",
			Data: nil,
		}
		u.ServeJSON()
		return
	}
	u.Data["json"] = Msg{
		Code: 200,
		Msg:  "登录成功",
		Data: user,
	}
	u.ServeJSON()
	return
}

func (u *UserControllers) Add() {
	rid, _ := u.GetInt("rid")
	pid, _ := u.GetInt("pid")
	if rid == 0 || pid == 0 {
		u.Data["json"] = Msg{
			Code: 0,
			Msg:  "角色或权限ID为空",
			Data: nil,
		}
		u.ServeJSON()
		return
	}
	RolePerm := models.RolePerm{
		Rid: rid,
		Pid: pid,
	}
	err := models.DB.Create(&RolePerm).Error
	if err != nil {
		u.Data["json"] = Msg{
			Code: 0,
			Msg:  "添加失败",
			Data: nil,
		}
		u.ServeJSON()
		return
	} else {
		u.Data["json"] = Msg{
			Code: 200,
			Msg:  "添加成功",
			Data: RolePerm,
		}
		u.ServeJSON()
	}
}

func (u *UserControllers) List() {
	uid, _ := u.GetInt("uid")
	if uid == 0 {
		u.Data["json"] = Msg{
			Code: 0,
			Msg:  "用户ID为空",
			Data: nil,
		}
		u.ServeJSON()
		return
	}
	var users []models.Role
	err := models.DB.Table("roles").
		Joins("left join user_roles on user_roles.rid=roles.id").
		Where("user_roles.uid=?", uid).
		//Select("roles.*").
		Debug().
		Find(&users).Error
	if err != nil {
		u.Data["json"] = Msg{
			Code: 0,
			Msg:  "查询失败",
			Data: nil,
		}
		u.ServeJSON()
		return
	} else {
		u.Data["json"] = Msg{
			Code: 200,
			Msg:  "查询成功",
			Data: users,
		}
		u.ServeJSON()
	}

}

func (u *UserControllers) List2() {
	rid, _ := u.GetInt("rid")
	if rid == 0 {
		u.Data["json"] = Msg{
			Code: 0,
			Msg:  "角色ID为空",
			Data: nil,
		}
		u.ServeJSON()
		return
	}
	var users []models.Perm
	err := models.DB.Table("perms").
		Joins("left join role_perms on role_perms.pid=perms.id").
		Where("role_perms.rid=?", rid).
		Debug().
		Find(&users).Error
	if err != nil {
		u.Data["json"] = Msg{
			Code: 0,
			Msg:  "查询失败",
			Data: nil,
		}
		u.ServeJSON()
		return
	} else {
		u.Data["json"] = Msg{
			Code: 200,
			Msg:  "查询成功",
			Data: users,
		}
		u.ServeJSON()
	}
}

func (u *UserControllers) Add2() {
	name := u.GetString("name")
	if name == "" {
		u.Data["json"] = Msg{
			Code: 0,
			Msg:  "名称不能为空",
			Data: nil,
		}
		u.ServeJSON()
		return
	}
	comp := models.Comp{
		Name: name,
	}
	err := models.DB.Create(&comp).Error
	if err != nil {
		u.Data["json"] = Msg{
			Code: 0,
			Msg:  "添加失败",
			Data: nil,
		}
		u.ServeJSON()
		return
	} else {
		u.Data["json"] = Msg{
			Code: 200,
			Msg:  "添加成功",
			Data: comp,
		}
		u.ServeJSON()
	}
}

func (u *UserControllers) Add3() {
	price, _ := u.GetInt("price")
	cid, _ := u.GetInt("cid")
	tid, _ := u.GetInt("tid")
	if price == 0 || cid == 0 || tid == 0 {
		u.Data["json"] = Msg{
			Code: 0,
			Msg:  "不能为空",
			Data: nil,
		}
		u.ServeJSON()
		return
	}
	user := models.CompTypes{
		Price: price,
		Cid:   cid,
		Tid:   tid,
	}
	err := models.DB.Create(&user).Error
	if err != nil {
		u.Data["json"] = Msg{
			Code: 0,
			Msg:  "添加失败",
			Data: nil,
		}
		u.ServeJSON()
		return
	} else {
		u.Data["json"] = Msg{
			Code: 200,
			Msg:  "添加成功",
			Data: user,
		}
		u.ServeJSON()
	}
}

func (u *UserControllers) List3() {
	page, _ := u.GetInt("page", 1)
	size, _ := u.GetInt("size", 5)
	offset := (page - 1) * size
	var user []models.Num
	var count int64
	models.DB.Count(&count)
	user_redis_key := "user_list3" + strconv.Itoa(page)
	cacheJson, _ := models.Rdb.Get(models.Ctx, user_redis_key).Result()
	if cacheJson == "" {
		models.DB.Table("comp_types").
			Joins("left join comps on comp_types.cid=comps.id").
			Joins("left join types on comp_types.tid=types.id").
			Select("comp_types.id as Id", "comp_types.price as Price", "comps.name as Cname", "types.name as Tname").
			Offset(offset).Limit(size).
			Debug().Find(&user)
		s, _ := json.Marshal(cacheJson)
		models.Rdb.Set(models.Ctx, user_redis_key, s, time.Hour*2)
	} else {
		json.Unmarshal([]byte(cacheJson), &user)
	}

	pastData := make(map[string]interface{})
	pastData["page"] = page
	pastData["size"] = size
	pastData["sum"] = count
	pastData["sile"] = user
	u.Data["json"] = Msg{
		Code: 200,
		Msg:  "查询成功",
		Data: pastData,
	}
	u.ServeJSON()
	return

}

func (u *UserControllers) List4() {
	id, _ := u.GetInt("id")
	if id == 0 {
		u.Data["json"] = Msg{
			Code: 0,
			Msg:  "ID不能为空",
			Data: nil,
		}
		u.ServeJSON()
		return
	}
	var users models.CompTypes
	user_redis_key := "user_list4" + strconv.Itoa(id)
	cacheJson, _ := models.Rdb.Get(models.Ctx, user_redis_key).Result()
	if cacheJson == "" {
		models.DB.Find(&users, id)
		s, _ := json.Marshal(users)
		models.Rdb.Set(models.Ctx, user_redis_key, s, time.Hour*2)
	} else {
		json.Unmarshal([]byte(cacheJson), &users)
	}

	u.Data["json"] = Msg{
		Code: 200,
		Msg:  "查询成功",
		Data: users,
	}
	u.ServeJSON()
	return

}
